package com.tourlaid.mamagerment.ao.wx.impl

import com.github.pagehelper.Page
import com.github.pagehelper.PageHelper
import com.tourlaid.mamagerment.ao.wx.StoreThoughtsAo
import com.tourlaid.mamagerment.dto.HomeDto
import com.tourlaid.mamagerment.entity.ResponseData
import com.tourlaid.mamagerment.service.StoreImageService
import com.tourlaid.mamagerment.service.StoreService
import com.tourlaid.mamagerment.service.ThoughtService
import com.tourlaid.mamagerment.vo.HomeVo
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.util.CollectionUtils
import javax.annotation.Resource

/**
 * Created by Administrator on 2017/8/17.
 */
@Service("storeThoughtsAo")
class StoreThoughtsAoImpl : StoreThoughtsAo {

    override fun queryThoughts(storeId: Long, pageNo: Int): ResponseData<MutableList<MutableList<HomeVo>>> {
        var list: MutableList<MutableList<HomeVo>> = mutableListOf()
        var responseData = ResponseData(list)
        PageHelper.startPage<HomeDto>(pageNo, 10, true)
        var page = thoughtService!!.queryByShoreId(storeId) as Page<HomeDto>
        responseData.totalCount = page.getTotal()
        var resultList = page.getResult()
        if (CollectionUtils.isEmpty(resultList)) {
            return responseData
        }
        val length = resultList!!.size
        for (i in 0..length - 1) {
            if (i % 2 == 0) {
                if (i + 1 >= length) {
                    break;
                }
                var inList: MutableList<HomeVo> = mutableListOf()
                var homeVo = HomeVo()
                BeanUtils.copyProperties(resultList.get(i), homeVo)
                inList.add(homeVo)
                var homeVo2 = HomeVo()
                BeanUtils.copyProperties(resultList.get(i + 1), homeVo2)
                inList.add(homeVo2)
                list.add(inList)
            }
        }
        return responseData
    }

    @Resource
    val storeService: StoreService? = null
    @Resource
    val thoughtService: ThoughtService? = null
    @Resource
    val storeImageService: StoreImageService? = null

    override fun getSoreInfo(storeId: Long): MutableMap<String, String?> {
        val store = storeService!!.getById(storeId)
        val map = mutableMapOf(
                "image" to store.image,
                "storeName" to store.storeName
        )
        return map
    }

    override fun queryImages(storeId: Long, imageType: Int): List<String> {
        return storeImageService!!.queryImages(storeId, imageType)
    }

}